<?php

namespace App\Models;

use App\User;
use Encore\Admin\Traits\AdminBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jcc\LaravelVote\CanBeVoted;

/**
 * Class News
 * @package App\Models
 * @mixin \Eloquent
 */
class News extends Model
{
    use AdminBuilder, SoftDeletes, CanBeVoted;

    protected $primaryKey = "id";

    protected $vote = User::class;

    protected $guarded = [];

    /**
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     *
     */
    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

    public function detail()
    {
        return $this->hasOne(NewsDetail::class, 'news_id');
    }

    public function contents()
    {
        return $this->belongsTo(NewsDetail::class);
    }

    public function media()
    {
        return $this->belongsTo(Media::class, 'media_id');
    }

    public function getCoverAttribute($key)
    {
        return config('admin.upload.host') . $key . "_710x470";
    }

    public static function newsStatus()
    {
        $status = [
            'on' => ['value' => 1, 'text' => '启用', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'default'],
        ];

        return $status;
    }

    public static function newsIsHost()
    {
        $status = [
            'on' => ['value' => 1, 'text' => '推荐', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'default'],
        ];

        return $status;
    }

    public static function newsIsIndex()
    {
        $status = [
            'on' => ['value' => 1, 'text' => '首页显示', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'default'],
        ];

        return $status;
    }
}
